package com.pengtu.gsj.service.impl.yh;

import java.text.ParseException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.yh.TecConEvalutionDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.yh.TecConEvalution;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.service.yh.TecconevalutionService;
import com.pengtu.gsj.utils.DateUtils;

/**
 * 
 * 
 * TecconevalutionServiceImpl
 * 
 * 2017年4月18日 下午4:47:30
 * 
 * @version 1.0.0
 * 
 */
@Transactional
@Service
public class TecconevalutionServiceImpl implements TecconevalutionService {

	@Autowired
	private TecConEvalutionDao tecconevalutionDao;
	
	@Autowired
	private SystemManageService systemManageService;

	@Override
	public List<TecConEvalution> getTecconevalutions(QueryFilter queryFilter) {
		return tecconevalutionDao.find(queryFilter);
	}

	@Override
	public Page<TecConEvalution> findTecconevalutions(
			Page<TecConEvalution> page, QueryFilter queryFilter) {
		return tecconevalutionDao.findPage(page, queryFilter);
	}

	@Override
	public TecConEvalution getTecconevalution(String id) {
		return tecconevalutionDao.get(id);
	}

	@Override
	public void saveTecconevalution(TecConEvalution tecconevalution) {
		tecconevalutionDao.save(tecconevalution);
	}

	@Override
	public void deleteTecconevalution(String id) {
		tecconevalutionDao.delete(id);

	}

	/**
	 * 删除检查记录以及对应的附件记录
	 */
	@Override
	public void deleteTecconevalution(TecConEvalution tecConEvalution) {
		if (tecConEvalution.getAttachments() != null) {
			for (Attachment attachment : tecConEvalution.getAttachments()) {
				systemManageService.deleteAttachment(attachment);
			}
		}
		tecconevalutionDao.delete(tecConEvalution);
	}

	@Override
	public List<TecConEvalution> getCheckData(String startTime, String endTime)
			throws ParseException {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("testYear", startTime, MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("testYear",  endTime, MatchType.LE);
		}
		return tecconevalutionDao.find(queryFilter);
	}

	@Override
	public boolean checkTestYear(String testYear, String id) {
		return tecconevalutionDao.checkProperty("testYear", testYear, id);
	}

}
